$(document).ready(function () {
    query();
})

/*查询全部学生*/
var query = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.studentList",
        "sAjaxSource": "http://localhost:8080/student/queryStudent",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10' id='delete'>删除</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp":"stuSex",
                "mRender": function(data) {
                    var value = "男";
                    if (data==1){
                        value = "女";
                    }
                    return value;
                }
            },
            {"mDataProp": "stuGrade"},
            {"mDataProp": "stuPhone"},
            {"mDataProp": ""},
        ],
    })
};

$(document).on('click', '#delete', function () {
    //获取$stuNum
    var $stuNum = $(this).parents("tr").children(0).eq(0).text();
    console.log($stuNum);
    layer.confirm('确定将此学生移除吗？',{
        btn:['确认','取消']
    },function () {
        $.ajax({
            url: "http://localhost:8080/student/deleteStudent",
            type: "GET",
            async: true,
            data: "stuNum=" + $stuNum,
            dataType: "json",
            success: function () {
                layer.alert('操作成功!', function(){
                    location.reload();
                });
            },
            error: function () {
                layer.alert("操作失败!");
            }
        });
    })
});